package com.share.thread;

/**
 * @desc:
 * @author:caifan
 * @date:2022/4/3
 */
public class VisibilityDemo01 {
     int a = 10;
     int b = 20;

     static volatile boolean flag = true;

    public static void main(String[] args) {
        VisibilityDemo01 demo01 = new VisibilityDemo01();

        while (flag) {
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                demo01.change();
            }).start();
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                demo01.print();
                if (demo01.a == 10) {
                    flag = false;
                }
            }).start();
        }
    }

    private void change() {
        a = 30;
        b = a;
    }

    private void print() {
        System.out.println("b=" + b + ", a=" + a);
    }
}
